package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.dao.PowerDao;
import com.njtech.pet.entity.Power;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.PowerService;
import com.njtech.pet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by m1870 on 2020/4/15.
 */

@Controller
@RequestMapping("/power")
public class PowerController {

    @Autowired
    PowerService powerService;
@Autowired
    UserService userService;
    /**
     * 添加权限
     * @param uid  用户ID
     * @param power 权限
     * @param state 状态
     * @return 权限
     */
    @RequestMapping("/addPower")
    @ResponseBody
    public String addPower(@RequestParam Integer uid,@RequestParam String power,@RequestParam String state){
        Power newPower=new Power();

        //用作判断
        Power power1=new Power();
        power1.setPower(power);

        User user=userService.selectByUserId(uid);

        Map<String, Object> resultMap = new HashMap<>();
        int num=powerService.selectRowsByUserAndPower(user,power1,false);

        if(user==null) {
            resultMap.put("msg","没有该用户");
        }
        else if(num>0){

            resultMap.put("msg","管理员已存在");

        }else if(Objects.equals(user.getRole(), "管理员") || Objects.equals(user.getRole(), "客服")){

//            userService.updateUser(user);
            //生成新的power
            newPower.setUser(user);
            newPower.setPower(power);
            newPower.setState(state);
            newPower.setUpdateDate(new Date());

            powerService.insert(newPower);

            resultMap.put("msg","添加成功");
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 修改权限
     * @param powerId 权限ID
     * @param newState 新的状态
     * @param newPower 新的权限
     * @return 修改行数
     */

    @RequestMapping("/editPower")
    @ResponseBody
    public String editPower(@RequestParam Integer powerId,@RequestParam String newState,@RequestParam String newPower){

        //获取该条权限
        Power power1=new Power();
        power1=powerService.selectByPowerId(powerId);

        Map<String, Object> resultMap = new HashMap<>();
        if(power1==null) {
            resultMap.put("msg","没有该条权限");
        }else if(Objects.equals(power1.getPower(), newPower) && Objects.equals(power1.getState(), newState)){
            resultMap.put("msg","不能重复修改");
        } else {
            power1.setPower(newPower);
            power1.setState(newState);
            power1.setUpdateDate(new Date());
            powerService.updateByPower(power1);

            resultMap.put("msg", "修改成功");
        }

        return JSON.toJSONString(resultMap);

    }

    /**
     * 查询对应用户，不同power状态下在起止日期间的记录
     * @param uid 用户ID
     * @param state 选择的权限状态
     * @return 权限集合
     */
    @RequestMapping("/searchPower")
    @ResponseBody
    public String selectByUserIdAndPowerState(@RequestParam Integer uid,@RequestParam String state){

        User user = new User();

        user.setId(uid);

        List<Power> list=powerService.selectByUserAndPowerState(user, state);

        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("msg", list);

        return JSON.toJSONString(resultMap);
    }


    /**
     * 显示权限
     * @param powerId 权限ID
     * @return 权限
     */
    @RequestMapping("/showPower")
    @ResponseBody
    public String selectByPowerId(@RequestParam  Integer powerId){

       Power power= powerService.selectByPowerId(powerId);

        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("msg", power);

        return JSON.toJSONString(resultMap);
    }

}
